//
//  HXBluetoothDeviceLinkViewController.m
//  HongxuanBluetooth
//
//  Created by 广州弘轩科技发展有限公司 on 2023/8/1.
//

#import "HXBluetoothDeviceLinkViewController.h"

@interface HXBluetoothDeviceLinkViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;//
@property (strong, nonatomic) BlueToothManager *blueToothManager;//蓝牙
@property (strong, nonatomic) BlueToothManager *towblueToothManager;//蓝牙
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (assign, nonatomic) NSInteger dissdindex;//选中当前的设备
@property (strong, nonatomic) UILabel *devicenamelb;//显示链接的设备名字
@property (strong, nonatomic) UILabel *idnamelb;//显示设备ID
@property (assign, nonatomic) BOOL isdelloc;
@property (strong, nonatomic) NSMutableArray *myDevices;
@property (strong, nonatomic) NSMutableArray *userDemyDevices;
@property (strong, nonatomic) NSMutableArray *lbdataarray;//链接蓝牙数据
//@property (strong, nonatomic) NSMutableArray *memorylinkArray;//存储链接蓝牙 不能连接
@property (strong, nonatomic) HXBluetoothConnectionDataModel *model;

//@property (strong, nonatomic) NSMutableArray *bluetoothddataArray;//存储链接蓝牙能连接

@property (strong, nonatomic) UIImageView *clicktodisconnectvive;

@property (assign, nonatomic) BOOL duankai;
@property (strong, nonatomic) NSString *nameid;

@property (strong, nonatomic) NSMutableArray *linkuuidarray;//链接的uuid
@property (strong, nonatomic) NSString *brokenlinkuuid;//断开的uuid；

@end

@implementation HXBluetoothDeviceLinkViewController
-(void)viewWillAppear:(BOOL)animated{
    NSLog(@"加载viewWillAppear");
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"加载viewDidLoad");
//    :@"DisconnectBluetoothdevice"
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnectDevice:) name:@"disconnectDevice" object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnectDevice) name:@"disconnectDevice" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceHomepagedisconnect:) name:@"deviceHomepagedisconnect" object:nil];
    
    
    self.fd_prefersNavigationBarHidden = YES;
    self.isdelloc = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.dissdindex = 0;//默认第一个
    self.lbdataarray = [[NSMutableArray alloc] init];
    GCDSinglePerson *gcdPerson = [GCDSinglePerson sharedPerson];
    self.model = [[HXBluetoothConnectionDataModel alloc] init];
//    [self getVersionNumber];
    NSLog(@"kStatusBarHeight :%f kTabBarHeight :%f kNavHeight:%f",kStatusBarHeight,kTabBarHeight ,kNavHeight);

    UIImageView *tabr = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight-44, winWidth, 44)];
//    tabr.backgroundColor = [UIColor yellowColor];
    NSLog(@"winHeight:%f winWidth:%f",winHeight,winWidth);
    [self.view addSubview:tabr];
    UIImageView *linkageinterface = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 44)];
//    linkageinterface.image = [UIImage imageNamed:@"linkageinterface"];
    [tabr addSubview:linkageinterface];
    
   
    
    self.blueToothManager = [BlueToothManager shareInstance];
    self.blueToothManager.autoConnect = YES;
    [self.blueToothManager StartScan];//打开蓝牙
    
//    self.towblueToothManager = [BlueToothManager shareInstance];
//    self.towblueToothManager.autoConnect = YES;
//    [self.towblueToothManager StartScan];//打开蓝牙
    
    UIImageView *equipmentbjview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"up"]];
    equipmentbjview.frame = CGRectMake(43, kNavHeight+31, winWidth-86, 202);
    equipmentbjview.userInteractionEnabled = YES;
    [self.view addSubview:equipmentbjview];

    
    
    UIImageView *deviceIconview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"deviceicon"]];
    [equipmentbjview addSubview:deviceIconview];
    [deviceIconview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(26);
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(33);
    }];
    
    UILabel *devicenamelb = [[UILabel alloc] init];
    devicenamelb.text = @"device name";
    devicenamelb.font = [UIFont systemFontOfSize:16];
    devicenamelb.textColor = [UIColor redColor];
    self.devicenamelb = devicenamelb;
    [equipmentbjview addSubview:devicenamelb];
    [devicenamelb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(deviceIconview.mas_right).offset(7);
        make.top.mas_equalTo(35);
    }];
    
    UIImageView *signalview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"devicesignal"]];
    [equipmentbjview addSubview:signalview];
    [signalview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(24);
        make.width.mas_equalTo(49);
        make.height.mas_equalTo(24);
    }];
    
    UILabel *idnamelb = [[UILabel alloc] init];
    idnamelb.text = @"device uuid";
    idnamelb.font = [UIFont systemFontOfSize:16];
    idnamelb.textColor = [UIColor redColor];
    self.idnamelb = idnamelb;
    [equipmentbjview addSubview:idnamelb];
    [idnamelb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(36);
        make.right.mas_equalTo(-36);
        make.top.mas_equalTo(82);
        make.height.mas_equalTo(12);
    }];
    
    UIImageView *line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lien"]];
    [equipmentbjview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(124);
    }];
//    已链接
    UIImageView *clicktodisconnectvive = [[UIImageView alloc] init];
    clicktodisconnectvive.image = [UIImage imageNamed:@"clicktoconnect"];
    [equipmentbjview addSubview:clicktodisconnectvive];
    clicktodisconnectvive.userInteractionEnabled = YES;
    self.clicktodisconnectvive = clicktodisconnectvive;
    [clicktodisconnectvive mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-26);
        make.height.mas_equalTo(29);
        make.width.mas_equalTo(169);
        make.centerX.mas_equalTo(equipmentbjview.mas_centerX);
        
    }];
//    断开链接按钮
    UIButton *brokenlinkbt = [UIButton buttonWithType:UIButtonTypeSystem];
    [clicktodisconnectvive addSubview:brokenlinkbt];
//    brokenlinkbt.backgroundColor = [UIColor greenColor];
    [brokenlinkbt addTarget:self action:@selector(brokenlink) forControlEvents:UIControlEventTouchUpInside];
    [brokenlinkbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.size.mas_equalTo(clicktodisconnectvive);
    }];

    
//    UIImageView *linkStateview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"linkState"]];
//    [equipmentbjview addSubview:linkStateview];
//    [linkStateview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.
//    }];
    
    UIImageView *choosedeviceview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"choosedevice"]];
    [self.view addSubview:choosedeviceview];
    
    [choosedeviceview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(equipmentbjview.mas_bottom).offset(19);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(13);
        make.width.mas_equalTo(146);
    }];
    
#pragma mark 蓝牙列表
    UIImageView *tablebj = [[UIImageView alloc] init];
    tablebj.image = [UIImage imageNamed:@"devicelistbj"];
    NSInteger width = 0;
    if(winHeight<=736){
        width = 290;
    }else{
        width = 335;
    }
    
    [self.view addSubview:tablebj];
    self.view.userInteractionEnabled = YES;
    tablebj.userInteractionEnabled = YES;
    [tablebj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(choosedeviceview.mas_bottom).offset(10);
        make.left.mas_equalTo(43);
        make.right.mas_equalTo(-43);
        make.height.mas_equalTo(width+2);
    }];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(1, 1, winWidth-88, width) style:UITableViewStylePlain];
//    self.tableView.style = UITableViewStylePlain;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.layer.cornerRadius = 6;
    self.tableView.layer.masksToBounds = YES;
    self.tableView.backgroundColor = [UIColor blackColor];
    
    self.tableView.showsHorizontalScrollIndicator = YES;
    self.tableView.showsVerticalScrollIndicator = YES;
    self.tableView.indicatorStyle=UIScrollViewIndicatorStyleWhite;

//    self.tableView.backgroundView = [UIImage imageNamed:@"devicelistbj"];
    [self.tableView registerClass:[HXBluetoothSearchListCell class] forCellReuseIdentifier:@"cell"];
    [tablebj addSubview:self.tableView];
    
//    [SVProgressHUD showWithStatus:NSLocalizedString(@"蓝牙连接中", nil) ];
    
    WeakSelf
    self.blueToothManager.refershDeviceList = ^(NSMutableArray * _Nonnull list) {
        NSLog(@"蓝牙设备列表:%@",list);
        NSString *listuuid = [NSString stringWithFormat:@"%@",list[0][2]];
        NSLog(@"蓝牙设备列表返回来的 uuid%@",listuuid);
        NSLog(@"weakSelf.dataArr :%@",weakSelf.dataArr);
        BOOL  include = NO;
        if (weakSelf.dataArr.count>0) {
            
            NSMutableArray *dar = [[NSMutableArray alloc] initWithArray:list];
            NSLog(@"dar :%@",dar);
            
//            [weakSelf.dataArr addObjectsFromArray:list];
//            // array2 是去掉相同元素后的数组
//            NSMutableArray *array2 = [NSMutableArray array];
//
//            for (NSString *str in weakSelf.dataArr) {//遍历数组1
//                if (![array2 containsObject:str]) {//判断数组2中有没有该元素,如果没有就添加到数组2中,该步骤就是去掉数组中的重复元素
//                    [array2 addObject:str];
//                    NSLog(@"去重的数据%@",array2);
//                    NSLog(@"新获取到的数据%@",list);
//                    weakSelf.dataArr = [NSMutableArray arrayWithArray:array2];
//                }
//            }
//
//            [weakSelf.tableView reloadData];
            
            for (int i = 0; i<weakSelf.dataArr.count; i ++) {
                NSMutableArray *data = weakSelf.dataArr[i];
                if(data.count == 3){
                    if([data[2] isEqualToString:listuuid]){
                        include = YES;//包含
                        NSLog(@"包含3");
                    }

                }
                if(data.count == 2){
                    NSString *str = [NSString stringWithFormat:@"%@",data[1][@"uuid"]];
                    if([str isEqualToString:listuuid]){
                        include = YES;//包含
                        NSLog(@"包含2");
                    }

                }

               

            }
            if(include){

            }else{
                [weakSelf.dataArr addObjectsFromArray:list];
                [weakSelf.tableView reloadData];

            }
            
        }else{
            weakSelf.dataArr = list;
            [weakSelf.tableView reloadData];

        }
        
        
        
//        weakSelf.dataArr = list;
//        [weakSelf.tableView reloadData];

        
        
        
    };
//    self.towblueToothManager.refershDeviceList = ^(NSMutableArray * _Nonnull list) {
//        NSLog(@"第二个蓝牙设备列表:%@",list);
//
//
//
//
//
//    };
    
    self.blueToothManager.successfulConnectionBlock = ^(NSMutableArray * _Nonnull list) {
        NSLog(@"链接成功666：%@",list);
        [[NSNotificationCenter defaultCenter]postNotificationName:@"linkSuccess" object:nil userInfo:@{@"list":list}];
        
        NSDictionary *dict = [NSDictionary dictionaryWithDictionary:list[1]];
        NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dict[@"advertisementData"]];
        [mudic setObject:@"1" forKey:@"kCBAdvDataislink"];
//            self.dataArr[self.dissdindex] = @[self.dataArr[self.dissdindex][0],mudic];
        NSDictionary *nutadict = @{@"uuid":list[1][@"uuid"],@"advertisementData":mudic,@"name":list[1][@"name"]};
        
        
        
        list = @[list[0],nutadict];
        
        NSLog(@"链接成功修改后：%@",list);
        weakSelf.lbdataarray = list;
        NSLog(@"weakSelf.lbdataarray  :%@",weakSelf.lbdataarray );
        NSLog(@"uuid :%@",list[1][@"uuid"]);
        NSString* linkuuid = [NSString stringWithFormat:@"%@",list[1][@"uuid"]];
//        是否包含了
        if([weakSelf.linkuuidarray containsObject:linkuuid]){
            NSLog(@"hx包含了");
        }else{
            NSLog(@"hx没包含");
            [weakSelf.linkuuidarray addObject:linkuuid];
        }
        
        NSLog(@"设备链接状态5");
        weakSelf.clicktodisconnectvive.image = [UIImage imageNamed:@"clicktodisconnect"];
        NSString *uuid = [NSString stringWithFormat:@"%@",list[1][@"uuid"]];
//        防止重复连接添加
        if(weakSelf.memorylinkArray.count<1){
            [weakSelf.memorylinkArray addObject:list[1]];
            [weakSelf.bluetoothddataArray addObject:list];
        }else{
            for (int i = 0; i < weakSelf.memorylinkArray.count; i ++ ) {
                if([uuid isEqualToString:weakSelf.memorylinkArray[i][@"uuid"]]){
//                    return;
                }else{
                    [weakSelf.memorylinkArray addObject:list[1]];
                    [weakSelf.bluetoothddataArray addObject:list];
                }
            }
        }
//        [weakSelf.memorylinkArray addObject:list[1]];
        
        
//        [weakSelf.tableView reloadData];
        NSLog(@"蓝牙链接成功数据：%@",weakSelf.memorylinkArray);
        
        NSDictionary * dicar = list[1][@"advertisementData"];
        weakSelf.devicenamelb.text = dicar[@"kCBAdvDataLocalName"];
        weakSelf.devicenamelb.textColor = [UIColor greenColor];
        NSString *arrl = dicar[@"kCBAdvDataServiceUUIDs"];
        NSLog(@"arrl :%@",arrl);
        NSString *nameid =  [arrl substringToIndex:13];//截取下标3之前的字符串
        self.nameid = nameid;
        NSLog(@"nameid:%@",nameid);
        weakSelf.idnamelb.text = [NSString stringWithFormat:@"%@",nameid];
        weakSelf.idnamelb.textColor = [UIColor greenColor];
        
        [weakSelf.tableView reloadData];
        [weakSelf obtainElectricity];//重置时间
//        [weakSelf obtainElectricity];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           // 在子线程中执行需要延时的代码
           [NSThread sleepForTimeInterval:1.0]; // 休眠0.8秒钟

           dispatch_async(dispatch_get_main_queue(), ^{
              // 回到主线程中执行剩余操作
//               [weakSelf obtainElectricity];//重置时间
//               [weakSelf resendTime];//手表跳到对应的时间
           });
        });

        
        
        
    };
    
    self.blueToothManager.characteristicBlock = ^(CBCharacteristic * _Nonnull characteristic) {
      
        NSLog(@"蓝牙发送需要的数据 %@",characteristic);
        NSLog(@"weakSelf.lbdataarray 2 %@", weakSelf.lbdataarray);
        
        
    };
    
    
    
    self.blueToothManager.automaticLinkBlock = ^{
        
        [weakSelf obtainElectricity];
        NSLog(@"自动链接返回");
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           // 在子线程中执行需要延时的代码
           [NSThread sleepForTimeInterval:2.5]; // 休眠0.8秒钟

           dispatch_async(dispatch_get_main_queue(), ^{
              // 回到主线程中执行剩余操作

               [weakSelf back];
           });
        });
        
    };
    
    
#pragma mark 设备断开了
//    ConnectionFailureBlock
    
    self.blueToothManager.ConnectionFailureBlock = ^(NSString * _Nonnull uuid) {
      
        NSLog(@"设备断开了");
        NSLog(@"断开的对应设备uuid：%@",uuid);
        NSLog(@"设备链接状态6");
       
        
        weakSelf.clicktodisconnectvive.image = [UIImage imageNamed:@"clicktoconnect"];
        NSLog(@"weakSelf.dataArr :%@",weakSelf.dataArr);

//        NSLog(@"weakSelf.dataArr[weakSelf.dissdindex]:%@",weakSelf.dataArr[weakSelf.dissdindex]);
        if (weakSelf.dataArr.count > 0) {
            [weakSelf.dataArr removeObject:weakSelf.dataArr[weakSelf.dissdindex]];
            weakSelf.devicenamelb.text = @"device name";
            weakSelf.idnamelb.text = @"device uuid";
            weakSelf.devicenamelb.textColor = [UIColor redColor];
            weakSelf.idnamelb.textColor = [UIColor redColor];
            [weakSelf.tableView reloadData];
            
        }

        [weakSelf.blueToothManager StartScan];//打开蓝牙
        
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"bluetoothDisconnect" object:nil userInfo:@{@"uuid":uuid}];
    };
    
 
   
    self.blueToothManager.datastrBlock = ^(NSString * _Nonnull datastr) {
        NSLog(@"datastr:%@",datastr);
        NSArray *Arr = [NSArray array];
        Arr = [datastr componentsSeparatedByString:@"0x585703"];
        NSLog(@"componentsSeparatedByString========%@",Arr);
        NSString *results2 = [Arr[1] substringToIndex:2];
        NSLog(@"results2:%@",results2);
        int dinal = [XHbluetoolUtils hexadecimalto10:results2];
        NSLog(@"dinal :%d",dinal);

    };
    
    
    [self BottomFunction];
    
//    SafeAreaInsets
    
    [self loadMyDevices];
    
    if([themeColors isEqualToString:@"default"]){
        linkageinterface.image = [UIImage imageNamed:@"linkageinterface"];
        equipmentbjview.image = [UIImage imageNamed:@"up"];
        equipmentbjview.backgroundColor = [UIColor blackColor];
        self.tableView.backgroundColor = [UIColor blackColor];
        tablebj.image = [UIImage imageNamed:@"devicelistbj"];
        tablebj.backgroundColor = [UIColor blackColor];
    }else{
        linkageinterface.image = [UIImage imageNamed:@"linkageintermid"];
        
        equipmentbjview.image = [UIImage imageNamed:@""];
        equipmentbjview.backgroundColor = midnightRGB;
        equipmentbjview.layer.cornerRadius = 8;
        equipmentbjview.layer.masksToBounds = YES;
        
        self.tableView.backgroundColor = midnightRGB;
        tablebj.image = [UIImage imageNamed:@""];
        tablebj.backgroundColor = midnightRGB;
        tablebj.layer.cornerRadius = 8;
        tablebj.layer.masksToBounds = YES;
    }
//    主界面跳转过来的
    if(self.isSkipOrNot){
        NSLog(@"dataArr :%@ \n ",self.dataArr);
        self.dataArr = self.storageArray;
//        NSLog(@"%ld",self.dataArr.count);
        NSLog(@"\n dataArr:%@ 长度%ld" ,self.dataArr,self.dataArr.count);
        for (int i = 0 ; i <self.dataArr.count; i ++) {
            NSLog(@"跳转过来的：%@",self.dataArr[i][1]);
            NSString* linkuuid = [NSString stringWithFormat:@"%@",self.dataArr[i][1][@"uuid"]];
            if([self.linkuuidarray containsObject:linkuuid]){
                NSLog(@"hx1包含了");
            }else{
                NSLog(@"hx1没包含");
                [self.linkuuidarray addObject:linkuuid];
            }
        }
        if(self.dataArr.count>0){
            
        
//        if ([self.disconnectDevice isEqualToString:@"1"]) {
//            self.clicktodisconnectvive.image = [UIImage imageNamed:@"clicktoconnect"];
//            NSLog(@"weakSelf.dataArr :%@",self.dataArr);
//            [self.dataArr removeObject:self.dataArr[self.dissdindex]];
//            self.devicenamelb.text = @"device name";
//            self.idnamelb.text = @"device uuid";
//            self.devicenamelb.textColor = [UIColor redColor];
//            self.idnamelb.textColor = [UIColor redColor];
//        }
            
        }
        [self.tableView reloadData];
    }
    
//    requestTimeoutBlock
#pragma mark - 链接请求超时
//    链接请求超时
    self.blueToothManager.requestTimeoutBlock = ^(NSMutableArray * _Nonnull list) {
        NSLog(@"self.dissdindex 选中的当前index %ld",(long)weakSelf.dissdindex);
        weakSelf.devicenamelb.textColor = [UIColor redColor];
        weakSelf.idnamelb.textColor = [UIColor redColor];
        if(weakSelf.dataArr.count == 0 ){
            return;
        }
        NSLog(@"self.dataArr%@",weakSelf.dataArr[weakSelf.dissdindex]);
        NSMutableArray *array = weakSelf.dataArr[weakSelf.dissdindex];
        NSString *uuid ;
        if(array.count == 2){
            
            NSDictionary *dict = [NSDictionary dictionaryWithDictionary:weakSelf.dataArr[weakSelf.dissdindex][1]];
            NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dict[@"advertisementData"]];
            uuid = [NSString stringWithFormat:@"%@",dict[@"uuid"]];
            [mudic setObject:@"0" forKey:@"kCBAdvDataislink"];
//            self.dataArr[self.dissdindex] = @[self.dataArr[self.dissdindex][0],mudic];
            NSDictionary *nutadict = @{@"uuid":weakSelf.dataArr[weakSelf.dissdindex][1][@"uuid"],@"advertisementData":mudic,@"name":weakSelf.dataArr[weakSelf.dissdindex][1][@"name"]};
            
            weakSelf.dataArr[weakSelf.dissdindex] = @[weakSelf.dataArr[weakSelf.dissdindex][0],nutadict];
            
            NSLog(@"赋值后的 self.dataArr[self.dissdindex] %@", weakSelf.dataArr[weakSelf.dissdindex]);
        }else{
            
            NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:weakSelf.dataArr[weakSelf.dissdindex][1]];
            [mudic setObject:@"0" forKey:@"kCBAdvDataislink"];
            weakSelf.dataArr[weakSelf.dissdindex]= @[weakSelf.dataArr[weakSelf.dissdindex][0],mudic,weakSelf.dataArr[weakSelf.dissdindex][2]];
            NSLog(@" 赋值 self.dataArr[self.dissdindex] :%@",weakSelf.dataArr[weakSelf.dissdindex]);
            uuid = [NSString stringWithFormat:@"%@",weakSelf.dataArr[weakSelf.dissdindex][2]];
        }
        NSLog(@"self.linkuuidarray :%@",weakSelf.linkuuidarray);
        [weakSelf.linkuuidarray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isEqualToString:uuid]) {
                [weakSelf.linkuuidarray removeObject:obj];
                
            }
        }];
        [weakSelf.tableView reloadData];
    };
}
#pragma mark 获取app版本号
- (void)getVersionNumber{
    
    //    1、填写自己App的ID http://itunes.apple.com/lookup?id=xxxxxxxxxxx
        NSString * strurl = [[NSString alloc] initWithFormat:@"https://itunes.apple.com/lookup/id6469992349"];//替换为自己App的ID
        NSURLSession *session=[NSURLSession sharedSession];
        NSURL *url = [NSURL URLWithString:strurl];
        
        //2.创建可变的请求对象
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
            //4.解析数据
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//            NSLog(@"dict:%@ response:%@",dict ,response);
            NSArray * results = dict[@"results"];
            NSDictionary * dic = results.firstObject;
            NSString * lineVersion = dic[@"version"];//版本号
            NSString * releaseNotes = dic[@"releaseNotes"];//更新说明
            NSString * trackViewUrl = dic[@"trackViewUrl"];//链接
            NSLog(@"App store版本号:%@",lineVersion);
            NSLog(@"更新说明:%@",releaseNotes);
            NSLog(@"App下载链接:%@",trackViewUrl);
            
    //        5、获取本地版本
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            // 本地app版本
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            NSLog(@"本地app版本 :%@",app_Version);
    //       6、比较版本信息
            if ([lineVersion floatValue] > [app_Version floatValue]) {
    //            7、回到主线程进行后续操作
//                dispatch_async(dispatch_get_main_queue(), ^{
////                    [self setGengxinUI];
//
//                });
                NSLog(@"App Store有最新版本了");
                NSLog(@"^^%@",app_Version);
            }else{
    //            7、回到主线程进行后续操作
//                dispatch_async(dispatch_get_main_queue(), ^{
////                    [SOSVC SOSToolTipShow:@"提示" message:@"已是最新版本" cancelTitle:@"确定" otherTitle:nil];
//                    NSLog(@"已经是最新版本了");
//                });
                NSLog(@"已经是最新版本了");
            }
            
        }];
        
        //3.执行任务
        [dataTask resume];
}
#pragma mark 蓝牙对应的删除
-(void)disconnectDevice:(NSNotification *)notification{
    NSDictionary *userInfo = notification.userInfo;
    NSString * uuid = [userInfo objectForKey:@"disconnectDevice"];
    NSLog(@"删除了对应的uuid ：%@",uuid);

}
- (void)deviceHomepagedisconnect:(NSNotification *)notification{
   
    NSDictionary *userInfo = notification.userInfo;
    NSString * uuid = [userInfo objectForKey:@"uuid"];
    NSLog(@"首页断开要删除对应的uuid %@",uuid);
    self.clicktodisconnectvive.image = [UIImage imageNamed:@"clicktoconnect"];
    NSLog(@"weakSelf.dataArr :%@",self.dataArr);

    if (self.dataArr.count > 0) {
//        [self.dataArr removeObject:self.dataArr[self.dissdindex]];
        for (int i = 0 ; i < self.dataArr.count ; i ++) {
            NSLog(@"%@",self.dataArr);
//            if([self.linkuuidarray containsObject:uuid]){
//                NSLog(@"hx1包含了");
//            }else{
//                NSLog(@"hx1没包含");
//                [self.dataArr removeObject:self.dataArr[i]];
//            }
        }
        self.devicenamelb.text = @"device name";
        self.idnamelb.text = @"device uuid";
        self.devicenamelb.textColor = [UIColor redColor];
        self.idnamelb.textColor = [UIColor redColor];
        [self.tableView reloadData];
        
    }

    [self.blueToothManager StartScan];//打开蓝牙
}
// 获取蓝牙设备
- (void)loadMyDevices {

//    NSMutableArray*_myDevices ;
    
    if (!_myDevices) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyDevices"];
        if (!data) {
            data = [NSData data];
        }
        _myDevices = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if (!_myDevices) {
            _myDevices = [NSMutableArray array];
        }
        NSLog(@"设备：%@", _myDevices);
        if(self.isSkipOrNot){
//            self.dataArr = _myDevices;
//            [self.tableView reloadData];
        }
    }
}
- (NSMutableArray *)linkuuidarray{
    
    if(!_linkuuidarray){
        _linkuuidarray = [[NSMutableArray alloc] init];
        
    }
    return _linkuuidarray;
}
- (NSMutableArray *)memorylinkArray{
    
    if(!_memorylinkArray){
        
        _memorylinkArray = [[NSMutableArray alloc] init];
    }
    return _memorylinkArray;
}
//bluetoothddataArray
- (NSMutableArray *)bluetoothddataArray{
    
    if(!_bluetoothddataArray){
        
        _bluetoothddataArray = [[NSMutableArray alloc] init];
    }
    return _bluetoothddataArray;
}
- (NSMutableArray *)sparelinkArray{
    
    if(!_sparelinkArray){
        _sparelinkArray = [[NSMutableArray alloc] init];
    }
    return _sparelinkArray;
}
-(void)BottomFunction{
    
    UIImageView *statebj = [[UIImageView alloc] init];
    if([themeColors isEqualToString:@"default"]){
        statebj.backgroundColor = [UIColor blackColor];
    }else{
        statebj.backgroundColor = midnightRGB;
    }
    [self.view addSubview:statebj];
    [statebj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-34);
        make.height.mas_equalTo(50);
    }];

    
#pragma mark 返回入口
    UIImageView *backview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back"]];
    backview.userInteractionEnabled = YES;
    [self.view addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(33);
        make.height.mas_equalTo(36);
        make.left.mas_equalTo(26);
    }];
    
    UIButton *backbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:backbt];
    [backbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(backview);
    }];
    
    
#pragma mark 链接入口
    UIImageView *scanview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scan"]];
    scanview.userInteractionEnabled = YES;
    [self.view addSubview:scanview];
    [scanview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(32);
        make.height.mas_equalTo(36);
        make.right.mas_equalTo(-26);
    }];

    UIButton *scanbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [scanbt addTarget:self action:@selector(scan) forControlEvents:UIControlEventTouchUpInside];
    [scanview addSubview:scanbt];
    [scanbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(scanview);
    }];
    
    
    UIImageView *bluetherview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bluether"]];
    bluetherview.userInteractionEnabled = YES;
    [self.view addSubview:bluetherview];
    [bluetherview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-52);
        make.height.mas_equalTo(13);
    }];
    
//    BlueToothManager *bluetooth = [[BlueToothManager alloc] init];
//    [bluetooth StartScan];
    
}
-(void)back{
    
    NSLog(@"蓝牙连接数据：%@  %@",self.memorylinkArray,self.bluetoothddataArray);
    if(self.bluetoothddataArray.count<1){
        self.bluetoothddataArray =self.storageArray;
    }
//    NSLog(@"myDevices:%@",[BlueToothManager shareInstance].myDevices);
    if (self.isSkipOrNot) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        self.isdelloc = YES;
        [self respecifyTheRootView];//更改根视图

        
    }
    
    
}
-(void)scan{
    NSLog(@"点击了scan");
//    [self.blueToothManager StartScan];//打开蓝牙
#pragma mark - 测试版本，写字版入口

    
    

}
#pragma mark - 获取时间
-(void)obtainElectricity{

    //    获取最后的时间
    Byte send[] = {0x58,0x57,0x03,0x0B,0x00,0x00,0x00,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
    [self.blueToothManager writeData:sendData];
    
    NSLog(@"获取最后的时间blueToothManager: %@",self.blueToothManager);
    //    链接
//    self.blueToothManager.datastrBlock = ^(NSString * _Nonnull datastr) {
//        NSLog(@"获取最后的时间： %@",datastr);
//    };
    self.blueToothManager.datalasttimeBlock = ^(NSString * _Nonnull lasttime) {
        NSLog(@"获取最后的时间： %@",lasttime);
        NSArray *Arr = [NSArray array];
        Arr = [lasttime componentsSeparatedByString:@"0x585703"];
//        NSLog(@"获取最后的时间：%@",Arr);
        NSString *time = [Arr[1] substringWithRange:NSMakeRange(4, 2)];
        NSString *minute = [Arr[1] substringWithRange:NSMakeRange(6, 2)];
        
//        字节 10-字节 13:芯片唯一ID 设备号
        NSString *chipid = [Arr[1] substringWithRange:NSMakeRange(14, 6)];
        NSLog(@"\n时:%@\n分:%@\n芯片id:%@",time,minute,chipid);
        
//        int dinal = [XHbluetoolUtils hexadecimalto10:results2];
//        hexadecimalto10
        if([XHbluetoolUtils hexadecimalto10:time]<10){
            time = [NSString stringWithFormat:@"0%d",[XHbluetoolUtils hexadecimalto10:time]];
        }else{
            time = [NSString stringWithFormat:@"%d",[XHbluetoolUtils hexadecimalto10:time]];
        }
        if([XHbluetoolUtils hexadecimalto10:minute]<10){
            minute = [NSString stringWithFormat:@"0%d",[XHbluetoolUtils hexadecimalto10:minute]];
        }else{
            minute = [NSString stringWithFormat:@"%d",[XHbluetoolUtils hexadecimalto10:minute]];
        }
        
        NSString *timeshij = [NSString stringWithFormat:@"%@:%@",time,minute];
        
//        NSLog(@"存储的时间：%@",timeshij);
        NSLog(@"%@",self.myDevices);
        //    存入蓝牙时间
        [[NSUserDefaults standardUserDefaults] setObject:timeshij forKey:@"Bluetoothtime"];
        //这里建议同步存储到磁盘中，但是不是必须的
        [[NSUserDefaults standardUserDefaults] synchronize];
    };
}
- (void)resendTime{
    
    //    存入蓝牙时间NSString *arr= [[NSUserDefaults standardUserDefaults] objectForKey:@"theme"];
    NSString *resendTime= [[NSUserDefaults standardUserDefaults] objectForKey:@"untraveledTime"];
    if(kStringIsEmpty(resendTime)||[resendTime isEqualToString:@"无"]){
        NSLog(@"显示无。");
        return;
    }
    NSLog(@"resendTime :%@",resendTime);
    NSArray *array = [resendTime componentsSeparatedByString:@":"];
    NSString * time = [NSString stringWithFormat:@"%@",array[0]];
    NSString * hour = [NSString stringWithFormat:@"%@",array[1]];
    int data = time.intValue;
    int tow = hour.intValue;
    NSLog(@"%d",data);
    data = [XHbluetoolUtils intTransitionhex16:data];
    if(data>12){
        data = data - 12;
    }
    
    NSLog(@"12进制时间：%d",data);
    tow = [XHbluetoolUtils intTransitionhex16:tow];
    Byte send[] = {0x58,0x57,0x03,0xff,0x00,data,tow,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
//    self.isArrive = NO;//发送时no 到指定位置折为yes
    [self.blueToothManager writeData:sendData];
    
    [[NSUserDefaults standardUserDefaults] setObject:@"无" forKey:@"untraveledTime"];
    //这里建议同步存储到磁盘中，但是不是必须的
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    //    存入蓝牙时间
    [[NSUserDefaults standardUserDefaults] setObject:resendTime forKey:@"Bluetoothtime"];
    //这里建议同步存储到磁盘中，但是不是必须的
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    
}
-(void)brokenlink{
    NSLog(@"点击了链接");
//    if(self.dataArr.count<1){
//        return;
//    }
//    NSMutableArray *arr = self.dataArr[self.dissdindex];
//    [self.blueToothManager connectWithPeripheral:arr[0] advertisementData:arr[1] macString:arr[2]];
    
    NSLog(@"断开了设备： %@",self.devicenamelb.text);
//    [_mgr cancelPeripheralConnection:fourConnectedPeripheral[showPage]];
//    [[self.blueToothManager centralManager] cancelPeripheralConnection:(nonnull CBPeripheral *)];
    
#pragma mark - 先测试跳出 记得取消 return

    if([self.devicenamelb.text isEqualToString:@"device name"]){
        NSLog(@"请选择对应的设备链接");
        [SVProgressHUD showErrorWithStatus:@"please select device"];
        return;
    }
    
    
    
    NSMutableArray *arr = self.dataArr[self.dissdindex];
    NSLog(@"arr CBPeripheral :%@",arr[0]);
    
    self.duankai = YES;
    
    NSLog(@"断开了设备数据：%@",self.dataArr[self.dissdindex]);
    NSString *kCBAdvDataislink ;
    NSString *brokenlinkuuid;
    if(arr.count == 2){
        self.brokenlinkuuid  = [NSString stringWithFormat:@"%@",arr[1][@"uuid"]];//断开的uuid；
        brokenlinkuuid = [NSString stringWithFormat:@"%@",arr[1][@"uuid"]];//断开的uuid
        kCBAdvDataislink = [NSString stringWithFormat:@"%@",arr[1][@"advertisementData"][@"kCBAdvDataislink"]];
        
    }else{
        self.brokenlinkuuid  = [NSString stringWithFormat:@"%@",self.dataArr[self.dissdindex][2]];//断开的uuid；
        brokenlinkuuid = [NSString stringWithFormat:@"%@",self.dataArr[self.dissdindex][2]];//断开的uuid
        kCBAdvDataislink = [NSString stringWithFormat:@"%@",arr[1][@"kCBAdvDataislink"]];
        
    }
    /*如果kCBAdvDataislink = 1 为链接状态，需要断开次蓝牙设备。
     断开蓝牙设备的时候 self.linkuuidarray 数组删除对应的uuid
     对应的按钮图标更改 字体颜色更改
     然后赋值kCBAdvDataislink = 0 重要。
     
     如果kCBAdvDataislink 不等于 1 为未链接状态，需要链接蓝牙设备。
     链接蓝牙设备的时候 self.linkuuidarray 数组添加对应的uuid
     对应的按钮图标更改 字体颜色更改
     然后赋值kCBAdvDataislink = 1 重要。
     */
    
    if([kCBAdvDataislink isEqualToString:@"1"]){
        NSLog(@"self.linkuuidarray :%@",self.linkuuidarray);
        [self.linkuuidarray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isEqualToString:brokenlinkuuid]) {
                [self.linkuuidarray removeObject:obj];
                
            }
        }];
//        clicktoconnect  clicktodisconnect
        NSLog(@"设备链接状态2");
        self.clicktodisconnectvive.image = [UIImage imageNamed:@"clicktoconnect"];
//        //断开了蓝牙连接
//        [self.blueToothManager disconnect];
        self.devicenamelb.textColor = [UIColor redColor];
        self.idnamelb.textColor = [UIColor redColor];
//        赋值kCBAdvDataislink = 0 重要。
        
        if(arr.count == 2){
            
            NSDictionary *dict = [NSDictionary dictionaryWithDictionary:self.dataArr[self.dissdindex][1]];
            NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dict[@"advertisementData"]];
            [mudic setObject:@"0" forKey:@"kCBAdvDataislink"];
//            self.dataArr[self.dissdindex] = @[self.dataArr[self.dissdindex][0],mudic];
            NSDictionary *nutadict = @{@"uuid":self.dataArr[self.dissdindex][1][@"uuid"],@"advertisementData":mudic,@"name":self.dataArr[self.dissdindex][1][@"name"]};
            
            self.dataArr[self.dissdindex] = @[self.dataArr[self.dissdindex][0],nutadict];
            
            NSLog(@"赋值后的 self.dataArr[self.dissdindex] %@", self.dataArr[self.dissdindex]);
        }else{
            
            NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:self.dataArr[self.dissdindex][1]];
            [mudic setObject:@"0" forKey:@"kCBAdvDataislink"];
            self.dataArr[self.dissdindex]= @[self.dataArr[self.dissdindex][0],mudic,self.dataArr[self.dissdindex][2]];
            NSLog(@" 赋值 self.dataArr[self.dissdindex] :%@",self.dataArr[self.dissdindex]);
        }
        
        
        NSLog(@"myDevices.count :%ld",self.myDevices.count);
        
        
        
        
        
        NSLog(@"删除前：self.myDevices :%@",self.myDevices);
        
        NSLog(@"[self.blueToothManager myDevices]; :%@",[self.blueToothManager myDevices]);
        NSMutableArray *datacout = [NSMutableArray arrayWithArray:self.dataArr[self.dissdindex]];
        
        if(datacout.count >2){

            NSString *uuid = [NSString stringWithFormat:@"%@",self.dataArr[self.dissdindex][2]];
            for (int i = 0; i<[self.blueToothManager myDevices].count; i++) {
                NSLog(@"是否要崩溃");
                NSLog(@"self.myDevices :%@",self.myDevices);
                if(self.myDevices.count>0){
                    if([uuid isEqualToString:[NSString stringWithFormat:@"%@",self.myDevices[i][@"uuid"]]]){
                        [[self.blueToothManager myDevices] removeObjectAtIndex:i];
                        NSLog(@"删除了对应uid ");
        //                [self.myDevices removeObject:self.myDevices[i]];
                    }
                }
//                NSLog(@"uuid :%@",self.myDevices[i][@"uuid"]);
//                if([uuid isEqualToString:[NSString stringWithFormat:@"%@",self.myDevices[i][@"uuid"]]]){
//                    [[self.blueToothManager myDevices] removeObjectAtIndex:i];
//                    NSLog(@"删除了对应uid ");
//    //                [self.myDevices removeObject:self.myDevices[i]];
//                }
            }
        }else{
            NSString *uuid = [NSString stringWithFormat:@"%@",self.dataArr[self.dissdindex][1][@"uuid"]];
//            NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyDevices"];
//            self.myDevices = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (int i = 0; i<[self.blueToothManager myDevices].count; i++) {
                if(self.myDevices.count>0){
                    if([uuid isEqualToString:[NSString stringWithFormat:@"%@",[self.blueToothManager myDevices][i][@"uuid"]]]){
                        [[self.blueToothManager myDevices] removeObjectAtIndex:i];
        //                [self.myDevices removeObject:self.myDevices[i]];
                    }
                }
//                NSLog(@"uuid :%@",self.myDevices[i][@"uuid"]);
//                if([uuid isEqualToString:[NSString stringWithFormat:@"%@",[self.blueToothManager myDevices][i][@"uuid"]]]){
//                    [[self.blueToothManager myDevices] removeObjectAtIndex:i];
//    //                [self.myDevices removeObject:self.myDevices[i]];
//                }
            }
        }
        
        NSLog(@"删除后：self.myDevices :%@",self.myDevices);
        NSLog(@"删除后： [self.blueToothManager myDevices]%@",[self.blueToothManager myDevices]);
        NSData *data = [NSJSONSerialization dataWithJSONObject:[self.blueToothManager myDevices] options:NSJSONWritingPrettyPrinted error:nil];
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"MyDevices"];
        NSLog(@"删除后2：self.myDevices :%@",[self.blueToothManager myDevices]);
//        [[NSUserDefaults standardUserDefaults] synchronize];
        
        NSData *dataa = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyDevices"];
        NSMutableArray *dataarrya = [NSJSONSerialization JSONObjectWithData:dataa options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"重新获取存储的数据：%@",dataarrya);
        
        NSLog(@"分针逆时针调");
//        brokenlinkuuid
        
        
       
        
//        直接进入休眠状态  相当于断开了
        Byte send[] = {0x58,0x57,0x03,0x0c,0x00,0x00,0x00,0x00};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
        [self.blueToothManager writeData:sendData];
        
        NSLog(@"完整的数组：%@ ，选中的数组：%@",self.dataArr,self.dataArr[self.dissdindex]);
//        休眠了，删除对应的数组信息
        [self.dataArr removeObject:self.dataArr[self.dissdindex]];
        self.devicenamelb.text = @"device name";
        self.idnamelb.text = @"device uuid";
        NSLog(@"断开删除的的数组：%@ ",self.dataArr);

        //断开了蓝牙连接
//        [self.blueToothManager disconnect];

        
    }else{
        
      /*  如果kCBAdvDataislink 不等于 1 为未链接状态，需要链接蓝牙设备。
        链接蓝牙设备的时候 self.linkuuidarray 数组添加对应的uuid
        对应的按钮图标更改 字体颜色更改
        然后赋值kCBAdvDataislink = 1 重要。
       */

        [self.linkuuidarray addObject:brokenlinkuuid];

//        链接设备
        NSLog(@"设备链接状态1");
        self.clicktodisconnectvive.image = [UIImage imageNamed:@"clicktodisconnect"];
        self.devicenamelb.textColor = [UIColor greenColor];
        self.idnamelb.textColor = [UIColor greenColor];
//        链接设备
        if(arr.count == 2){
            
            NSDictionary *dict = [NSDictionary dictionaryWithDictionary:self.dataArr[self.dissdindex][1]];
            NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dict[@"advertisementData"]];
            [mudic setObject:@"1" forKey:@"kCBAdvDataislink"];
            
            NSDictionary *nutadict = @{@"uuid":self.dataArr[self.dissdindex][1][@"uuid"],@"advertisementData":mudic,@"name":self.dataArr[self.dissdindex][1][@"name"]};
            
            self.dataArr[self.dissdindex] = @[self.dataArr[self.dissdindex][0],nutadict];
            NSLog(@"赋值后的 self.dataArr[self.dissdindex] %@", self.dataArr[self.dissdindex]);
            [self.blueToothManager connectWithPeripheral:arr[0] advertisementData:mudic macString:brokenlinkuuid];
            
        }else{
            
            NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:self.dataArr[self.dissdindex][1]];
            [mudic setObject:@"1" forKey:@"kCBAdvDataislink"];
            
            self.dataArr[self.dissdindex]= @[self.dataArr[self.dissdindex][0],mudic,self.dataArr[self.dissdindex][2]];
            
            NSLog(@" 赋值 self.dataArr[self.dissdindex] :%@",self.dataArr[self.dissdindex]);
            
            [self.blueToothManager connectWithPeripheral:arr[0] advertisementData:arr[1] macString:arr[2]];
        }
        
        [self.tableView reloadData];
    }
    
//    NSString *uuid = [NSString stringWithFormat:@"%@",self.dataArr[self.dissdindex][2]];
//    self.brokenlinkuuid = uuid;//断开的uuid；
  
    
    
   /* NSLog(@"myDevices.count :%ld",self.myDevices.count);
    NSMutableArray *datacout = [NSMutableArray arrayWithArray:self.dataArr[self.dissdindex]];
    if(datacout.count >2){
        NSString *uuid = [NSString stringWithFormat:@"%@",self.dataArr[self.dissdindex][2]];
        for (int i = 0; i<self.myDevices.count; i++) {
            
            NSLog(@"uuid :%@",self.myDevices[i][@"uuid"]);
            if([uuid isEqualToString:[NSString stringWithFormat:@"%@",self.myDevices[i][@"uuid"]]]){
                [self.myDevices removeObjectAtIndex:i];
                NSLog(@"删除了对应uid ");
//                [self.myDevices removeObject:self.myDevices[i]];
            }

            self.userDemyDevices = self.myDevices;
        }
    }else{
        NSString *uuid = [NSString stringWithFormat:@"%@",self.dataArr[self.dissdindex][1][@"uuid"]];
        
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyDevices"];
        self.myDevices = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        for (int i = 0; i<self.myDevices.count; i++) {
            
            NSLog(@"uuid :%@",self.myDevices[i][@"uuid"]);
            if([uuid isEqualToString:[NSString stringWithFormat:@"%@",self.myDevices[i][@"uuid"]]]){
                [self.myDevices removeObjectAtIndex:i];
//                [self.myDevices removeObject:self.myDevices[i]];
               
            }
            
            self.userDemyDevices = self.myDevices;
        }
        
    }
    
    NSLog(@"self.userDemyDevices.count :%ld  self.myDevices.count:%ld",self.userDemyDevices.count, self.myDevices.count);
    
    
    NSLog(@"删除后：self.myDevices :%@",self.myDevices);
    

    
    NSData *data = [NSJSONSerialization dataWithJSONObject:self.myDevices options:NSJSONWritingPrettyPrinted error:nil];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"MyDevices"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    */
    NSLog(@"_myDevices:%@",_myDevices);
    
    [self.tableView reloadData];
    
    
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    NSLog(@"%lu",(unsigned long)self.dataArr.count);
//    NSLog(@"self.dataArr == %@",self.dataArr);
    return self.dataArr.count;
    

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    HXBluetoothSearchListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSLog(@"数据结构是否改变：%@",self.dataArr[indexPath.row]);
    
    NSString *uuid;
//    NSDictionary *dict;
//    if(self.isSkipOrNot){
//        NSLog(@"数据结构1：%@",self.dataArr[indexPath.row]);
//
//
//        dict = [NSDictionary dictionaryWithDictionary:self.dataArr[indexPath.row][1]];
//        NSLog(@"字典：%@",dict);
//        if(self.duankai){
//            self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dict];
//            NSString *name = [NSString stringWithFormat:@"%@",dict[@"kCBAdvDataLocalName"]];
//            if(self.model.kCBAdvDataLocalName == nil){
//                self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dict[@"advertisementData"]];
//            }
//
//        }else{
//            NSArray *arr = self.dataArr[indexPath.row];
//            if (arr.count == 2) {
////                self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dict];
//                self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dict[@"advertisementData"]];
//            }else{
////                self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dict[@"advertisementData"]];
//                self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dict];
//            }
////            self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dict[@"advertisementData"]];
//            NSString *name = [NSString stringWithFormat:@"%@",dict[@"kCBAdvDataLocalName"]];
//            if(name !=nil){
//
//            }
//        }
////        self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dict[@"advertisementData"]];
//
//
//
//        NSString *kCBAdvDataLocalName = [NSString stringWithFormat:@"%@",self.model.kCBAdvDataLocalName];
//        if([kCBAdvDataLocalName isEqualToString:@"(null)"]){
//            self.devicenamelb.text  = dict[@"kCBAdvDataLocalName"];
//            self.devicenamelb.textColor = [UIColor greenColor];
//        }else{
//            self.devicenamelb.text = [NSString stringWithFormat:@"%@",self.model.kCBAdvDataLocalName];
//            self.devicenamelb.textColor = [UIColor greenColor];
//        }
////        self.devicenamelb.text = [NSString stringWithFormat:@"%@",self.model.kCBAdvDataLocalName];
//        NSLog(@"self.model.kCBAdvDataServiceUUIDs :%@",self.model.kCBAdvDataServiceUUIDs);
//
//        NSString *nameid =  [self.model.kCBAdvDataServiceUUIDs substringToIndex:13];//截取下标3之前的字符串
//        self.nameid = nameid;
//        self.idnamelb.text = [NSString stringWithFormat:@"%@",nameid];
////        self.idnamelb.textColor = [UIColor greenColor];
//        if(self.model.uuid == nil){
//            self.model.uuid = nameid;
//            if(nameid == nil){
//                NSLog(@"nameid nil ");
//                NSArray *arrl = dict[@"kCBAdvDataServiceUUIDs"];
//
//                NSString *strid = [NSString stringWithFormat:@"%@",arrl[0]];
//                NSString *nameid =  [strid substringToIndex:13];//截取下标3之前的字符串
//                self.idnamelb.text = [NSString stringWithFormat:@"%@",nameid];
////                self.idnamelb.textColor = [UIColor greenColor];
//                self.nameid = nameid;
//            }
//        }else{
//            self.model.uuid =dict[@"uuid"];
//        }
//        NSLog(@"kCBAdvDataLocalName :%@",self.model.kCBAdvDataLocalName);
//        NSArray *dataarry = self.dataArr[indexPath.row];
//        if(dataarry.count == 2){
//
//            uuid = [NSString stringWithFormat:@"%@",dataarry[1][@"uuid"]];
//            NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dict[@"advertisementData"]];
//
//            if([self.linkuuidarray containsObject:uuid]){
//                NSLog(@"跟链接成功的蓝牙uuid一致");
//                self.model.kCBAdvDataislink = @"1";
//                [mudic setObject:@"1" forKey:@"kCBAdvDataislink"];
//            }
//
//            if([uuid isEqualToString:self.brokenlinkuuid]){
//                self.model.kCBAdvDataislink = @"0";
//                self.brokenlinkuuid = @"";
//                [mudic setObject:@"0" forKey:@"kCBAdvDataislink"];
//            }
//
//
//
//            NSMutableDictionary *dicttow = [NSMutableDictionary dictionaryWithDictionary:dict];
//            [dicttow setObject:mudic forKey:@"advertisementData"];
//            self.dataArr[indexPath.row] = @[self.dataArr[indexPath.row][0],dicttow];
//
//        }else{
//            NSLog(@"数据结构1 长度3 :%@",dataarry);
//            uuid = [NSString stringWithFormat:@"%@",dataarry[2]];
//            NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dict];
//            if([self.linkuuidarray containsObject:uuid]){
//                NSLog(@"跟链接成功的蓝牙uuid一致");
//                self.model.kCBAdvDataislink = @"1";
//                [mudic setObject:@"1" forKey:@"kCBAdvDataislink"];
//            }
//
//            if([uuid isEqualToString:self.brokenlinkuuid]){
//                self.model.kCBAdvDataislink = @"0";
//                self.brokenlinkuuid = @"";
//                [mudic setObject:@"0" forKey:@"kCBAdvDataislink"];
//            }
//
//
//            self.dataArr[indexPath.row] = @[self.dataArr[indexPath.row][0],mudic,self.dataArr[indexPath.row][2]];
//        }
//
//        NSLog(@"uuid :%@",uuid);
////
////        if([uuid isEqualToString:self.linkuuid]){
////            NSLog(@"跟链接成功的蓝牙uuid一致");
////            self.model.kCBAdvDataislink = @"1";
////
////        }
////
////        if([uuid isEqualToString:self.brokenlinkuuid]){
////            self.model.kCBAdvDataislink = @"0";
////            self.brokenlinkuuid = @"";
////        }
//
//
//    }
//    else{
    NSLog(@"数据结构2：%@",self.dataArr[indexPath.row]);
    NSMutableArray *dataArray = self.dataArr[indexPath.row];
    if(dataArray.count == 2){
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.dataArr[indexPath.row][1][@"advertisementData"]];
        uuid =  [NSString stringWithFormat:@"%@",dic[@"kCBAdvDataServiceUUIDs"]];
        
        NSString * uuidone = [NSString stringWithFormat:@"%@",self.dataArr[indexPath.row][1][@"uuid"]];
        NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:self.dataArr[indexPath.row][1][@"advertisementData"]];

        if([self.linkuuidarray containsObject:uuidone]){
         
            [mudic setObject:@"1" forKey:@"kCBAdvDataislink"];
        }

        NSMutableDictionary *dicttow = [NSMutableDictionary dictionaryWithDictionary:dic];
        [dicttow setObject:mudic forKey:@"advertisementData"];
        
        NSDictionary *nutadict = @{@"uuid":self.dataArr[indexPath.row][1][@"uuid"],@"advertisementData":mudic,@"name":self.dataArr[indexPath.row][1][@"name"]};
        
//        self.dataArr[self.dissdindex] = @[self.dataArr[self.dissdindex][0],nutadict];
        self.dataArr[indexPath.row] = @[self.dataArr[indexPath.row][0],nutadict];
        NSLog(@"长度为2的数组赋值之后：%@",self.dataArr[indexPath.row]);
        
        
        self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:dic];
        
//        self.linkuuidarray
    }else{
        
        NSLog(@"self.dataArr[indexPath.row][1] %@",self.dataArr[indexPath.row][1]);
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.dataArr[indexPath.row][1]];
        NSLog(@"dic %@",dic);
        NSArray *arrl = dic[@"kCBAdvDataServiceUUIDs"];
        uuid =  [NSString stringWithFormat:@"%@",arrl[0]];

        
        NSString * uuidone = [NSString stringWithFormat:@"%@",self.dataArr[indexPath.row][2]];
        NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dic];
        if([self.linkuuidarray containsObject:uuidone]){
           
            [mudic setObject:@"1" forKey:@"kCBAdvDataislink"];
        }
        self.dataArr[indexPath.row] = @[self.dataArr[indexPath.row][0],mudic,self.dataArr[indexPath.row][2]];
        
        self.model = [HXBluetoothConnectionDataModel yy_modelWithDictionary:self.dataArr[indexPath.row][1]];
        NSLog(@"数据结构2 kCBAdvDataislink :%@",self.model.kCBAdvDataislink);
//    }
    }
    
    NSLog(@"model.kCBAdvDataislink :%@",self.model.kCBAdvDataislink);
    
    NSLog(@"kCBAdvDataLocalName :%@",self.model.kCBAdvDataLocalName);
    if(self.model.kCBAdvDataLocalName == nil){
//        cell.kCBAdvDataLocalName = dic[@"kCBAdvDataLocalName"];
//        self.devicenamelb.text
    
    }
    if([self.model.kCBAdvDataislink isEqualToString:@"1"]){
        self.devicenamelb.textColor = [UIColor greenColor];
        self.idnamelb.textColor = [UIColor greenColor];
        self.devicenamelb.text = self.model.kCBAdvDataLocalName;
        
        NSString *nameid =  [uuid substringToIndex:13];//截取下标3之前的字符串
        self.idnamelb.text = [NSString stringWithFormat:@"%@",nameid];
        
        NSLog(@"设备链接状态3");
        self.clicktodisconnectvive.image = [UIImage imageNamed:@"clicktodisconnect"];
    }else{
        self.devicenamelb.textColor = [UIColor redColor];
        self.idnamelb.textColor = [UIColor redColor];
        NSLog(@"设备链接状态4");
        self.clicktodisconnectvive.image = [UIImage imageNamed:@"clicktoconnect"];
    }
    
    cell.model = self.model;
//    cell.idnamestr = model.kCBAdvDataLocalName;
    cell.didselectindex = indexPath.row;
    

    cell.didseleBlock = ^(NSInteger didselet) {
        NSLog(@"点击了对应的下标：%ld",(long)didselet);
    };
    
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了对应蓝牙对应的下标%ld",(long)indexPath.row);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *btn = [cell viewWithTag:2];
    NSArray *dataarray = self.dataArr[indexPath.row];
    if(dataarray.count == 2){
        NSDictionary *dict = [NSDictionary dictionaryWithDictionary:dataarray[1]];
        NSDictionary *advertisementData = [NSDictionary dictionaryWithDictionary:dict[@"advertisementData"]];
        NSString *kCBAdvDataislink = [NSString stringWithFormat:@"%@",advertisementData[@"kCBAdvDataislink"]];
        if([kCBAdvDataislink isEqualToString:@"1"]){
            NSLog(@"kCBAdvDataislink 为1，已链接");
            self.devicenamelb.textColor = [UIColor greenColor];
            self.idnamelb.textColor = [UIColor greenColor];
            
        }else{
            NSLog(@"kCBAdvDataislink 为空，未链接");
            self.devicenamelb.textColor = [UIColor redColor];
            self.idnamelb.textColor = [UIColor redColor];
        }
        
    }else{
        NSString *kCBAdvDataislink = [NSString stringWithFormat:@"%@",dataarray[1][@"kCBAdvDataislink"]];
        if([kCBAdvDataislink isEqualToString:@"1"]){
            NSLog(@"kCBAdvDataislink 为1，已链接");
            self.devicenamelb.textColor = [UIColor greenColor];
            self.idnamelb.textColor = [UIColor greenColor];
            
        }else{
            NSLog(@"kCBAdvDataislink 为空，未链接");
            self.devicenamelb.textColor = [UIColor redColor];
            self.idnamelb.textColor = [UIColor redColor];
        }
    }

    
    self.dissdindex = indexPath.row;
    
//#pragma mark - 方便测试直接return
//    return;
    
 /*
    NSMutableArray *arr = self.dataArr[indexPath.row];
    [self.blueToothManager connectWithPeripheral:arr[0] advertisementData:arr[1] macString:arr[2]];
  */
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSLog(@"选中对应的蓝牙设备：%@",self.dataArr[indexPath.row]);
    [self updateDeviceInformation];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(NSMutableArray *)dataArr{
    if(!_dataArr){
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
    
}


-(void)dealloc{
////    //蓝牙停止扫描
//    [self.blueToothManager stopScan];
}

//选中设备，更新对应的显示信息
-(void)updateDeviceInformation{
    
//    数据源没数据处理
    if(self.dataArr.count<1){
        return;
    }
    
    NSMutableArray *arr = self.dataArr[self.dissdindex];
    if(arr.count == 2){
        NSString * linkuuid = [NSString stringWithFormat:@"%@",arr[1][@"uuid"]];
//        [self.linkuuidarray containsObject:uuid]
        if([self.linkuuidarray containsObject:linkuuid]){
            NSLog(@"hx2包含了");
        }else{
            NSLog(@"hx2没包含");
//            [self.linkuuidarray addObject:linkuuid];
        }
    }else{
        NSString *linkuuid = [NSString stringWithFormat:@"%@",arr[2]];
        if([self.linkuuidarray containsObject:linkuuid]){
            NSLog(@"hx3包含了");
        }else{
            NSLog(@"hx3没包含");
//            [self.linkuuidarray addObject:linkuuid];
        }
    }
    NSLog(@"点击对应的数据链接：%@",arr);
    if(self.isSkipOrNot){
        
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:arr[1][@"advertisementData"]] ;
        NSString *name = [NSString stringWithFormat:@"%@",arr[1][@"kCBAdvDataLocalName"]];
        if(name != nil){
            
            NSLog(@"字典数据为空");
            NSDictionary *dictow = [NSDictionary dictionaryWithDictionary:arr[1]] ;
            NSString *macStringtow;
            
            if(arr.count == 2){
                
                macStringtow = [NSString stringWithFormat:@"%@",arr[1][@"uuid"]];
                self.devicenamelb.text = dictow[@"advertisementData"][@"kCBAdvDataLocalName"];
//                self.devicenamelb.textColor = [UIColor greenColor];
                
            }else{
                macStringtow = [NSString stringWithFormat:@"%@",arr[2]];
                self.devicenamelb.text = dictow[@"kCBAdvDataLocalName"];
//                self.devicenamelb.textColor = [UIColor greenColor];
                
            }
            
//            [self.blueToothManager connectWithPeripheral:arr[0] advertisementData:dictow macString:macStringtow];
            

            NSString *strid = [NSString stringWithFormat:@"%@",dictow[@"kCBAdvDataServiceUUIDs"]];
            if(strid == nil){
                NSArray *arrl = dictow[@"kCBAdvDataServiceUUIDs"];
                
                NSString *strid = [NSString stringWithFormat:@"%@",arrl[0]];
                NSString *nameid =  [strid substringToIndex:13];//截取下标3之前的字符串
                self.idnamelb.text = [NSString stringWithFormat:@"%@",nameid];
//                self.idnamelb.textColor = [UIColor greenColor];
                self.nameid = nameid;
            }else{
                
                
                if(arr.count == 3){
                    NSArray *arrl = dictow[@"kCBAdvDataServiceUUIDs"];
                    
                    NSString *strid = [NSString stringWithFormat:@"%@",arrl[0]];
                    NSString *nameid =  [strid substringToIndex:13];//截取下标3之前的字符串
                    self.idnamelb.text = [NSString stringWithFormat:@"%@",nameid];
//                    self.idnamelb.textColor = [UIColor greenColor];
                    self.nameid = nameid;
                }else{
                    if(strid.length == 6){
                        NSString *uuid = [NSString stringWithFormat:@"%@",dictow[@"advertisementData"][@"kCBAdvDataServiceUUIDs"]];
                        NSString *nameid =  [uuid substringToIndex:13];//截取下标3之前的字符串
                        self.nameid = nameid;
                        self.idnamelb.text = nameid;
//                        self.idnamelb.textColor = [UIColor greenColor];
                    }else{
                        NSString *nameid =  [strid substringToIndex:13];//截取下标3之前的字符串
                        self.nameid = nameid;
                        self.idnamelb.text = nameid;
//                        self.idnamelb.textColor = [UIColor greenColor];
                    }

                }
                


            }
            
            
        }else{
            NSString *macString = [NSString stringWithFormat:@"%@",arr[1][@"uuid"]];
            
//            [self.blueToothManager connectWithPeripheral:arr[0] advertisementData:dic macString:macString];
            
            self.devicenamelb.text = dic[@"kCBAdvDataLocalName"];
//            self.devicenamelb.textColor = [UIColor greenColor];
            NSString *strid = [NSString stringWithFormat:@"%@",dic[@"kCBAdvDataServiceUUIDs"]];
            NSString *nameid =  [strid substringToIndex:13];//截取下标3之前的字符串
            self.nameid = nameid;
            self.idnamelb.text = nameid;
//            self.idnamelb.textColor = [UIColor greenColor];
        }
        
        

        
    }else{
        
        
        NSLog(@"arr :%@ arr0 :%@ arr1 :%@ arr2 :%@ ",arr ,arr[0],arr[1],arr[2]);
        
//        [self.blueToothManager connectWithPeripheral:arr[0] advertisementData:arr[1] macString:arr[2]];
       
        NSDictionary * dicar = self.dataArr[self.dissdindex][1];
        self.devicenamelb.text = dicar[@"kCBAdvDataLocalName"];
//        self.devicenamelb.textColor = [UIColor greenColor];
        NSArray *arrl = dicar[@"kCBAdvDataServiceUUIDs"];
        
        NSString *strid = [NSString stringWithFormat:@"%@",arrl[0]];
        NSString *nameid =  [strid substringToIndex:13];//截取下标3之前的字符串
        self.idnamelb.text = [NSString stringWithFormat:@"%@",nameid];
//        self.idnamelb.textColor = [UIColor greenColor];
        
    }

//    [self.tableView reloadData];
}

- (NSData *)hexToBytes:(NSString *)str{
    
    NSMutableData* data = [NSMutableData data];
    int idx;
    for (idx = 0; idx+2 <= str.length; idx+=2) {
    NSRange range = NSMakeRange(idx, 2);
    NSString* hexStr = [str substringWithRange:range];
    NSScanner* scanner = [NSScanner scannerWithString:hexStr];
    unsigned int intValue;
    [scanner scanHexInt:&intValue];
    [data appendBytes:&intValue length:1];
   }
   return data;
}
#pragma mark - 更改根视图
-(void)respecifyTheRootView{
//    if(self.memorylinkArray.count<1){
//        self.memorylinkArray = self.sparelinkArray;
//    }
    NSLog(@"更改根视图 self.dataArr %@",self.dataArr);
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    ViewController *mainView = [[ViewController alloc]init];
    mainView.lbdataarray = self.memorylinkArray;
    mainView.bluetoothddataArray = self.bluetoothddataArray;
//    mainView.model = self.model;
    
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:mainView];
    [window setRootViewController:navi];
    [window makeKeyAndVisible];
    
    
}
- (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}
//- (NSData*)ad_hexStringToData {
//    if (self.length % 2 != 0) {
//        return nil;
//    }
//    NSMutableData *mulData = [[NSMutableData alloc] init];
//    for (NSInteger i = 0; i < self.length; i += 2) {
//        NSString *hexChar = [self substringWithRange:NSMakeRange(i, 2)];
//        NSScanner *scanner = [[NSScanner alloc] initWithString:hexChar];
//        unsigned int anInt;
//        [scanner scanHexInt:&anInt];
//        NSData *data = [[NSData alloc] initWithBytes:&anInt length:1];
//        [mulData appendData:data];
//    }
//    return mulData;
//}
-(NSMutableArray *)myDevices{//取

    if (!_myDevices) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyDevices"];
        if (!data) {
            data = [NSData data];
        }
        _myDevices = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if (!_myDevices) {
            _myDevices = [NSMutableArray array];
        }
        
    }
    return _myDevices;
}
- (NSMutableArray *)userDemyDevices{
    if(!_userDemyDevices){
        
        _userDemyDevices = [NSMutableArray array];
    }
    return _userDemyDevices;
}
@end
